
<template>
  <div class="product-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>产品列表</span>
          <el-button type="primary" @click="getDataList">刷新</el-button>
        </div>
      </template>
      
      <el-table
        :data="productList"
        v-loading="loading"
        style="width: 100%"
        border
        stripe
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="产品名称" width="180" />
        <el-table-column prop="category" label="行业分类" width="120">
          <template #default="scope">
            <el-tag :type="getTagType(scope.row.category)">
              {{ scope.row.category }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="价格" width="120" sortable>
          <template #default="scope">
            ¥{{ scope.row.price.toLocaleString() }}
          </template>
        </el-table-column>
        <el-table-column prop="stock" label="库存" width="100" sortable />
        <el-table-column prop="description" label="产品描述" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button
              size="small"
              @click="handleDetail(scope.row.id)"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <el-pagination
        class="pagination"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    
    <el-dialog v-model="dialogVisible" title="产品详情" width="50%">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="产品名称">{{ productDetail.name }}</el-descriptions-item>
        <el-descriptions-item label="行业分类">
          <el-tag :type="getTagType(productDetail.category)">
            {{ productDetail.category }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="价格">¥{{ productDetail.price.toLocaleString() }}</el-descriptions-item>
        <el-descriptions-item label="库存">{{ productDetail.stock }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ productDetail.createTime }}</el-descriptions-item>
        <el-descriptions-item label="CPU">{{ productDetail.specs?.cpu }}</el-descriptions-item>
        <el-descriptions-item label="内存">{{ productDetail.specs?.memory }}</el-descriptions-item>
        <el-descriptions-item label="存储">{{ productDetail.specs?.storage }}</el-descriptions-item>
        <el-descriptions-item label="产品描述" :span="2">
          {{ productDetail.description }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getProductList, getProductDetail } from '../api/product'
import { ElMessage } from 'element-plus'

const productList = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const dialogVisible = ref(false)
const productDetail = ref({})

const getTagType = (category) => {
  const map = {
    'AI算力': 'success',
    '智能制造': 'primary',
    '新能源汽车': 'warning',
    '低空经济': 'danger'
  }
  return map[category] || ''
}

const getDataList = () => {
  loading.value = true
  const params = {
    page: currentPage.value,
    size: pageSize.value
  }
  getProductList(params).then(response => {
      productList.value = response.data.list
      total.value = response.total
    }).catch(() => {
      ElMessage.error('获取产品列表失败')
    }).finally(() => {
      loading.value = false
    })
}

const handleDetail = (id) => {
  getProductDetail(id)
    .then(response => {
      productDetail.value = response.data
      dialogVisible.value = true
    })
    .catch(() => {
      ElMessage.error('获取产品详情失败')
    })
}

const handleSizeChange = (val) => {
  pageSize.value = val
  getDataList()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  getDataList()
}

onMounted(() => {
  getDataList()
})
</script>

<style scoped>
.product-container {
  padding: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.pagination {
  margin-top: 20px;
  justify-content: flex-end;
}
</style>
